package com.kaili.basic.controller.web.v1;

import com.baomidou.mybatisplus.plugins.Page;
import com.kaili.basic.pojo.dto.BaseFinanceSubjectQueryDTO;
import com.kaili.basic.pojo.entity.BaseFinanceSubjectEntity;
import com.kaili.basic.pojo.vo.BaseFinanceSubjectExport;
import com.kaili.basic.service.BaseFinanceSubjectService;
import com.kaili.basic.version.ApiVersion;
import com.kaili.common.exception.RestException;
import com.kaili.common.response.CommonStatus;
import com.kaili.common.response.RestStatus;
import com.kaili.common.util.ExcelUtil;
import com.kaili.common.util.LoginDetailUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import javax.transaction.Transactional;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@ApiVersion(1)
@RestController
@RequestMapping("/{api_version}")
@Transactional
@Api(tags = "财务类别管理", description = "财务类别管理")
public class BaseFinanceSubjectController {

    @Autowired
    private BaseFinanceSubjectService baseFinanceSubjectService;


    @GetMapping("/financeSuject/orderNo")
    @ApiOperation(value = "查询科目编号")
    public Long queryFinanceSujectCode(){
        return baseFinanceSubjectService.queryFinanceSujectCode();
    }

    @GetMapping("/financeSuject")
    @ApiOperation(value = "分页查询财务类别")
    public Page<BaseFinanceSubjectEntity> queryPageList(@ModelAttribute BaseFinanceSubjectQueryDTO queryDTO){
        return baseFinanceSubjectService.queryPageList(new Page<BaseFinanceSubjectEntity>(queryDTO.getPage(),queryDTO.getPageSize()),queryDTO);
    }

    @GetMapping("/financeSuject/all")
    @ApiOperation(value = "查询所有财务类别")
    public List<BaseFinanceSubjectEntity> queryAllList(){
        return baseFinanceSubjectService.queryAllList();
    }

    @GetMapping("/financeSuject/export")
    @ApiOperation(value = "财务类别明细导出")
    public void export(HttpServletResponse response, @ModelAttribute BaseFinanceSubjectQueryDTO queryDto) throws RestException {
        List<BaseFinanceSubjectExport> list = baseFinanceSubjectService.export(queryDto);
        ExcelUtil.exportExcel(list,"财务类别管理","sheet1", BaseFinanceSubjectExport.class, UUID.randomUUID().toString()+".xls",response);
    }

    @PostMapping("/financeSuject")
    @ApiOperation(value = "新增财务类别")
    public RestStatus addFinanceSuject(@RequestBody BaseFinanceSubjectEntity dto){
        dto.setCreateTime(new Date());
        dto.setCreateBy(LoginDetailUtils.getCurrentEmployeeId().longValue());
        baseFinanceSubjectService.insert(dto);
        return CommonStatus.ADD_OK;
    }

    @PutMapping("/financeSuject")
    @ApiOperation(value = "修改财务类别")
    public RestStatus updateFinanceSuject(@RequestBody BaseFinanceSubjectEntity dto){
        baseFinanceSubjectService.updateById(dto);
        return CommonStatus.UPDATE_OK;
    }

    @DeleteMapping("/financeSuject/{id}")
    @ApiOperation(value = "删除财务类别")
    public RestStatus deleteFinanceSuject(@ApiParam("财务类别id")@PathVariable("id") long id){
        baseFinanceSubjectService.deleteById(id);
        return CommonStatus.DELETE_OK;
    }
}


